unit PlanejMensal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit,
  cxCurrencyEdit, FMTBcd, DB, DBClient, Provider, SqlExpr,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData;

type
  TfrmPlanejMensal = class(TForm)
    tlFluxo: TcxDBTreeList;
    tlFluxocxDBTreeListColumn2: TcxDBTreeListColumn;
    sqlPlanej: TSQLDataSet;
    dspPlanej: TDataSetProvider;
    cdsPlanej: TClientDataSet;
    dsPlanej: TDataSource;
    sqlPlanejSequencial: TIntegerField;
    sqlPlanejCodigo: TStringField;
    sqlPlanejMae: TStringField;
    sqlPlanejDescricao: TStringField;
    sqlPlanejTipo: TStringField;
    sqlPlanejValor: TFMTBCDField;
    cdsPlanejSequencial: TIntegerField;
    cdsPlanejCodigo: TStringField;
    cdsPlanejMae: TStringField;
    cdsPlanejDescricao: TStringField;
    cdsPlanejTipo: TStringField;
    cdsPlanejValor: TFMTBCDField;
    tlFluxocxDBTreeListColumn1: TcxDBTreeListColumn;
    sqlAux: TSQLDataSet;
    IntegerField1: TIntegerField;
    StringField1: TStringField;
    StringField2: TStringField;
    StringField3: TStringField;
    StringField4: TStringField;
    FMTBCDField1: TFMTBCDField;
    dspAux: TDataSetProvider;
    cdsAux: TClientDataSet;
    IntegerField2: TIntegerField;
    StringField5: TStringField;
    StringField6: TStringField;
    StringField7: TStringField;
    StringField8: TStringField;
    FMTBCDField2: TFMTBCDField;
    dsAux: TDataSource;
    procedure cdsPlanejAfterPost(DataSet: TDataSet);
    procedure cdsAuxAfterPost(DataSet: TDataSet);
    procedure cdsPlanejBeforeEdit(DataSet: TDataSet);
  private
    nValorAux:Double;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmPlanejMensal: TfrmPlanejMensal;

implementation
  Uses M_Principal;
{$R *.dfm}

procedure TfrmPlanejMensal.cdsPlanejAfterPost(DataSet: TDataSet);
var iSeq,x:Integer;
nValor:Double;
begin
  X:=0;
  with cdsPlanej do begin
    if ApplyUpdates(0) =0 then begin
      iSeq:=FieldByName('Sequencial').asInteger;
      nValor:=FieldByName('Valor').asFloat;
      if FieldByName('Mae').asString <> '' then begin
        cdsAux.Locate('Codigo',Trim(FieldByName('Mae').asString),[locaseinsensitive]);
        cdsAux.Edit;
        if nValorAux > nValor Then
          nValor := nValor-nValorAux;
        cdsAux.FieldByName('Valor').asFloat:=cdsAux.FieldByName('Valor').asFloat + nValor;
        cdsAux.Post;
        while cdsAux.FieldByName('Mae').asString <> '' do begin
          cdsAux.Locate('Codigo',Trim(cdsAux.FieldByName('Mae').asString),[locaseinsensitive]);
          cdsAux.Edit;
          cdsAux.FieldByName('Valor').asFloat:=cdsAux.FieldByName('Valor').asFloat + nValor;
          cdsAux.Post;
        end;
      end;
    end;
  end;
  cdsPlanej.Close;
  cdsPlanej.Open;
  cdsPlanej.Locate('Sequencial',iSeq,[locaseinsensitive]);
end;

procedure TfrmPlanejMensal.cdsAuxAfterPost(DataSet: TDataSet);
begin
  cdsAux.ApplyUpdates(0);
end;

procedure TfrmPlanejMensal.cdsPlanejBeforeEdit(DataSet: TDataSet);
begin
  nValorAux:=cdsPlanej.FieldByName('Valor').asFloat;
end;

end.
